﻿#include "FITKAcousticsVTKGraphObjectLoadConstraint.h"

// VTK
#include <vtkImageData.h>

// Graph and filter
#include "FITKAcousticsVTKCommons.h"
#include "FITKAcousticsVTKSharedDataManager.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractLoad.h"
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActorImage.h"

namespace Exchange
{
    FITKAcousticsVTKGraphObjectLoadConstraint::FITKAcousticsVTKGraphObjectLoadConstraint(Acoustics::FITKAcousticsAbstractLoad* constraint)
        : FITKAcousticsVTKGraphObjectPictureBase(constraint)
    {
        if (!constraint)
        {
            return;
        }

        // Initialize vtk objects.
        init();

        // Generate the data set.
        if (!generateDataSet())
        {
            return;
        }

        // Generate the nodes actor.
        generateGraph();

        // Update the visibility.
        updateVisibility();

        // Save the data type.
        m_graphInfo.Type = FITKAcousticsVTKCommons::LoadConstraintGraph;
    }

    void FITKAcousticsVTKGraphObjectLoadConstraint::init()
    {
        this->Superclass::init();

        // Copy the image data.
        m_picData->DeepCopy(FITKAcousticsVTKSharedDataManager::GetInstance()->getLoadConstraintPicData(true, false));
        m_picDataHighlight->DeepCopy(FITKAcousticsVTKSharedDataManager::GetInstance()->getLoadConstraintPicData(true, true));
        m_picDataDisable->DeepCopy(FITKAcousticsVTKSharedDataManager::GetInstance()->getLoadConstraintPicData(false, false));
        m_picDataHighlightDisable->DeepCopy(FITKAcousticsVTKSharedDataManager::GetInstance()->getLoadConstraintPicData(false, true));
    }

    bool FITKAcousticsVTKGraphObjectLoadConstraint::getDataVisibility()
    {
        bool dataVisible = this->Superclass::getDataVisibility();

        // Get the runtime settings for visibility.
        bool showLoad = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_LOADS, true);
        bool showLoadConst = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_LOAD_CONSTRAINT, true);
        return dataVisible && showLoad && showLoadConst;
    }

    void FITKAcousticsVTKGraphObjectLoadConstraint::update(bool forceUpdate)
    {
        Q_UNUSED(forceUpdate);
        Acoustics::FITKAcousticsAbstractLoad* load = dynamic_cast<Acoustics::FITKAcousticsAbstractLoad*>(_dataObj);
        if (load == nullptr && m_fActorImg == nullptr)return;

        bool enabled = load->getEnabledState();
        bool highlighted = isHighlighting();
        if (enabled && !highlighted)
        {
            m_fActorImg->SetInputData(m_picData);
        }
        else if (enabled && highlighted)
        {
            m_fActorImg->SetInputData(m_picDataHighlight);
        }
        else if (!enabled && !highlighted)
        {
            m_fActorImg->SetInputData(m_picDataDisable);
        }
        else if (!enabled && highlighted)
        {
            m_fActorImg->SetInputData(m_picDataHighlightDisable);
        }

        // Update visibility.
        updateVisibility();

        // Update by camera.
        updateByCamera();
    }

    bool FITKAcousticsVTKGraphObjectLoadConstraint::generateDataSet()
    {
        // Save the position.
        Acoustics::FITKAcousticsAbstractLoad* constraint = dynamic_cast<Acoustics::FITKAcousticsAbstractLoad*>(_dataObj);
        if (!constraint)
        {
            return false;
        }

        constraint->getConnectionLocation(m_pos3);

        // Update by camera.
        updateByCamera();

        return true;
    }

    bool FITKAcousticsVTKGraphObjectLoadConstraint::getDataEnabled()
    {
        Acoustics::FITKAcousticsAbstractLoad* constraint = dynamic_cast<Acoustics::FITKAcousticsAbstractLoad*>(_dataObj);
        if (!constraint)
        {
            return false;
        }

        return constraint->getEnabledState();
    }
}   // namespace Exchange